import logging
import time,os
from logging import handlers

from common.GetFilePath import filePath


class Log(object):
    def log(self):
        # 创建一个日志器
        logger = logging.getLogger("logger")
        # 设置日志输出的最低等级，低于当前等级就会忽略
        logger.setLevel(logging.INFO)

        if not logger.handlers:
            # 创建处理器
            # 1. 控制台日志处理器
            sh = logging.StreamHandler()
            # 2. 文件日志处理器

            self.filename = "../log/{}_log.log".format(time.strftime('%Y_%m_%d_%H_%M_%S',time.localtime()))

            # 日志输出到文件
            #fh = logging.FileHandler(filename="../log/{}_log.log".format(time.strftime('%Y_%m_%d_%H_%M_%S',time.localtime())),mode='a',encoding="utf-8")

            # 往文件里写入#指定间隔时间自动生成文件的处理器
            #th = handlers.TimedRotatingFileHandler(filename=filePath('log',self.filename), when='D', backupCount=3,encoding='utf-8')

            # 创建一个格式器
            formatter = logging.Formatter(format("%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s"))

            sh.setFormatter(formatter)
            #fh.setFormatter(formatter)
            #th.setFormatter(formatter)

            logger.addHandler(sh)
            # logger.addHandler(fh)
            #logger.addHandler(th)

        return logger



if __name__ == '__main__':
#log日志的调用
    #Log().log().info("执行返回code码%s" % (r.status_code))
    Log().log().info("执行返回code码%s" % ('123'))